<template>
  <div class="custom-report-list-main-container">
    <div class="report-list-container">
      <el-form :inline="true" style="height: 50px; float: right" size="small">
        <el-form-item>
          <el-input
            v-model="reportTitle"
            placeholder="请输入报表名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="reportName"
            placeholder="请输入报表英文标识"
          ></el-input>
        </el-form-item>
        <el-button size="medium" class="btn-icon" @click="getReportList">
          <span class="el-icon-search" ></span>
        </el-button>
        <el-button class="btn-icon" size="medium" @click="refreshForm">
          <span class="el-icon-refresh" ></span>
        </el-button>
      </el-form>
      <el-table
        v-loading="isLoading"
        :data="reportDatas"
        border
        style="width: 100%"
      >
        <el-table-column prop="title" label="报表名称" width="250">
          <template #default="scope">
            <span
              style="cursor: pointer"
            >
              {{ scope.row.title }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="chartLayoutName" label="报表标识" width="220">
        </el-table-column>
        <el-table-column prop="chartLayoutId" label="报表ID" width="220">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              @click="handleClickEdit(scope.row.chartLayoutName)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              @click="handleClickPreview(scope.row.chartLayoutName)"
              type="text"
              size="small"
              >预览</el-button
            >
            <el-button
              @click="handleClickDel(scope.row.chartLayoutId, scope.row.title)"
              type="text"
              size="small"
            >
              删除/初始化
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pageInfo.currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.totalRecord"
        style="margin-top: 5px"
        @size-change="sizeChange"
        @current-change="currentChange"
      > </el-pagination>
    </div>
    <el-dialog title="新增报表" v-model="dialogVisible" width="70%">
      <el-form size="medium">
        <el-form-item label="报表名称">
          <el-input
            style="width: 400px"
            v-model="newReportName"
            placeholder="请输入新报表名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="英文标识">
          <el-input
            style="width: 400px"
            v-model="chartLayoutName"
            placeholder="请输入新报表英文标识"
          ></el-input>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <span class="dialog-footer">
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import nt from '../../network'

export default {
  name: 'customReportList',
  data() {
    return {
      breadArr: [
        { name: '我的报表' },
        { name: '报表列表', url: '/business/reportmgr/customReportList' },
      ],
      isLoading: false,
      dialogVisible: false,
      reportDatas: [],
      newReportName: '',
      chartLayoutName: '',
      reportTitle: '',
      reportName: '',
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        totalRecord: 0,
      },
    }
  },
  methods: {
    refreshForm() {
      this.reportTitle = ''
      this.reportName = ''
    },
    handleAddReport() {
      if (!this.newReportName) {
        this.$message.error('请输入报告名称')
        return
      }
      if (!this.chartLayoutName) {
        this.$message.error('请输入报告英文标识')
        return
      }
      let reg = /^[A-Za-z]+$/
      if (!reg.test(this.chartLayoutName)) {
        this.$message.error('标识只能使用英文大小写')
        return
      }
      nt.doRequest({
        method: 'post',
        uri: '/paas/chart-layout/v1/chart/checkname',
        data: {
          params: this.chartLayoutName,
        },
        onSuccess: (resp) => {
          if (!resp.data.content) {
            nt.doRequest({
              method: 'post',
              uri: '/paas/chart-layout/v2/chart/chartLayout/save',
              data: {
                params: {
                  chartLayoutName: this.chartLayoutName,
                  children: [],
                  title: this.newReportName,
                },
              },
              onSuccess: (resp) => {
                this.dialogVisible = false
                this.handleClickEdit(this.chartLayoutName)
              },
            })
          } else {
            this.$message.error('报告英文标识已存在，请更换标识！')
          }
        },
      })
    },
    handleClickDel(chartLayoutId, title) {
      this.$confirm('是否删除或初始化“' + title + '”报表?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          nt.doRequest({
            method: 'POST',
            uri: '/paas/chart-layout/v2/chart/delete',
            data: {
              params: chartLayoutId,
            },
            onSuccess: (resp) => {
              this.$message.success('删除/初始化成功')
              this.getReportList()
            },
          })
        })
        .catch(() => {})
    },
    handleClickEdit(name) {
      this.$router.push({
        path: '/business/reportmgr/dragReport/' + name,
      })
    },
    handleClickPreview(name) {
      this.$router.push({
        path: '/business/reportmgr/previewReport/' + name,
        query: {
          fromList: true,
        },
      })
    },
    sizeChange(pageSize) {
      this.pageInfo.currentPage = 1
      this.pageInfo.pageSize = pageSize
      this.getReportList()
    },
    currentChange(currentPage) {
      this.pageInfo.currentPage = currentPage
      this.getReportList()
    },
    getReportList() {
      this.isLoading = true
      nt.doRequest({
        method: 'post',
        uri: '/paas/chart-layout/v1/chart/list-all',
        data: {
          params: {
            chartLayoutName: this.reportName,
            title: this.reportTitle,
          },
          pageInfo: this.pageInfo,
        },
        onSuccess: (resp) => {
          this.reportDatas = resp.data.content.list
          this.pageInfo = resp.data.content.pageInfo
          this.isLoading = false
        },
        onError: (err) => {
          this.isLoading = false
        },
      })
    },
  },

  mounted() {
    // this.getReportList()
  },
}
</script>

<style lang="scss" scoped>
@import './layout.scss';
</style>
